import { Dispatch } from 'redux'
import * as actionTypes from '../constants'
import {
    getGoodStuffListRequest,
    getGoodStuffTabsRequest
} from '@/api/request'

export const changeGoodStuffList = (data: []) => ({
    type: actionTypes.CHANGE_GOOD_STUFF_LIST,
    data
})

export const changeGoodStuffTabs = (data: string) => ({
    type: actionTypes.CHANGE_GOOD_STUFF_TABS,
    data
})

export const addGoodStuffList = (data: boolean) => ({
    type: actionTypes.ADD_GOOD_STUFF_LIST,
    data
})

export const changeHasMore = (data: boolean) => ({
    type: actionTypes.CHANGE_HAS_MORE,
    data
})

export const changeIsShowImage = (data: boolean) => ({
    type: actionTypes.CHANGE_IS_SHOW_IMAGE,
    data
})

export const changeImageShowList = (data: any[]) => ({
    type: actionTypes.CHANGE_IMAGE_SHOW_LIST,
    data
})

export const changeImageDefaultIndex = (data: string) => ({
    type: actionTypes.CHANGE_IMAGE_DEFAULT_INDEX,
    data
})

export const changeLoading = (data: boolean) => ({
    type: actionTypes.CHANGE_LOADING,
    data
})

export const getGoodStuffListData = (page: number, key: string, date: number, getMore?: boolean) => {
    return (dispatch:Dispatch)  => {
        (async () => {
            if (!getMore) {
                dispatch(changeGoodStuffList([]));
                dispatch(changeLoading(true))
            }
            let {data} = await getGoodStuffListRequest(page, key, date)
            if (data.content) {
                if (getMore) {
                    dispatch(addGoodStuffList(data.content))
                } else {
                    dispatch(changeLoading(false))
                    dispatch(changeGoodStuffList(data.content))
                }
            }
            if (data.hasMore == false || data.hasMore == true) {
                dispatch(changeHasMore(data.hasMore))
            }
        })()
    }
}

export const getGoodStuffTabsData = () => {
    return (dispatch:Dispatch)  => {
        (async () => {
            let {data} = await getGoodStuffTabsRequest()
            dispatch(changeGoodStuffTabs(data))
        })()
    }
}